/**
 * Created by chenweicheng on 9/7/17.
 */
'use strict';

import {PixelRatio, Dimensions, Platform, StatusBar} from 'react-native';
import * as px2dp from './px2dp'
import ScreenUtil from '../util/ScreenUtil'
import RNFS from 'react-native-fs';
export default {
    pr: PixelRatio.get(),
    screenWidth: Dimensions.get('window').width,
    screenHeight: Dimensions.get('window').height,

    btnActiveOpacity: 0.7,
    lastTimeStamp: 0,
    actionBar: {
        height: px2dp.px2dpH(49),
        backgroundColor: 'rgb(22,131,251)',
        fontSize: 16,
        fontColor: 'white'
    },
    text: {
        fontSize: 15
    },
    iosStatusBarHeight: Platform.OS === 'ios' ? (ScreenUtil.isIphoneX() ? 44 : 20) : StatusBar.currentHeight,
    headerStyle: {
        backgroundColor: '#ffffff',
        height: (Platform.OS === 'ios') ? px2dp.px2dpH(64) : px2dp.px2dpH(54),
        //  paddingBottom: (Platform.OS === 'ios') ? px2dp.px2dpH(0) : px2dp.px2dpH(10),
        elevation: 0,
        shadowOpacity: 0,
    },
    headerTitleCenter: {
        textAlign: 'center',
        flex: 1
    },

    headerTitleText: {
        fontFamily: 'PingFang SC',
        fontSize: 17,
        textAlign: "center",
        // fontWeight: 500,'normal'
        fontStyle: 'normal',
        // fontStretch: 'normal',
        // lineHeight: 'normal',
        // letterSpacing: 'normal','-0.3'
        color: '#1d1d1d',
    },
    statusBar: {
        backgroundColor: '#f9f9f9',
        barStyle: 'dark-content'
    },
    roundedButton: {
        width: px2dp.px2dpW(295),
        height: px2dp.px2dpH(48),
        borderRadius: px2dp.px2dpH(48) / 2,
        marginLeft: px2dp.px2dpW(40),
        marginRight: px2dp.px2dpW(40),
        justifyContent: "center"
    },
    roundedButtonText: {
        fontSize: 17,
        textAlign: "center",
        justifyContent: "center"
    },


    timeStampUtil(forWhat){
        console.log("=======" + forWhat + "=======")
        let time = (new Date()).valueOf();
        if (this.lastTimeStamp !== 0) {
            console.log("spend   time=" + (time - this.lastTimeStamp))
        }
        this.lastTimeStamp = time
        console.log("current time=" + this.lastTimeStamp)
    },
    getPorScreenWidth(){
        return (this.screenHeight > this.screenWidth) ? this.screenWidth : this.screenHeight
    },
    getLandScreenWidth(){
        return (this.screenWidth > this.screenHeight) ? this.screenWidth : this.screenHeight
    },
    getHexFromAscii(str){
        let ret = "";
        for (let i = 0; i < str.length / 2; i++) {
            console.log(str.substring(i * 2, i * 2 + 2), String.fromCharCode(parseInt(str.substring(i * 2, i * 2 + 2), 16)))
            ret = ret + String.fromCharCode(parseInt(str.substring(i * 2, i * 2 + 2), 16));
        }
        return ret
    },


    datainfo: {
        "start": 0,
        "timelength": 0,
        "rate": 0,
        "groups": 0
    },

    datatrue: false,

    releaseData(){
        this.yvalue1 = []
        this.yvalue2 = []
        this.yvalue3 = []
        this.air = []
        this.temp = []
        this.wet = []
        this.db = []
        this.wav = []
        this.timesecond = []
        this.timesecondformatdetail = []
        this.timesecondformathome = []
        this.homebottomtext = []
        this.datatrue = false

        this.yvalue1aver = 0;
        this.yvalue2aver = 0;
        this.yvalue3aver = 0;
        this.envaver = []


    },


    yvalue1: [],
    yvalue2: [],
    yvalue3: [],


    air: [],
    temp: [],
    wet: [],
    db: [],


    yvalue1aver: 0,
    yvalue2aver: 0,
    yvalue3aver: 0,
    envaver: [],

    wav: [],

    timesecond: [],
    timesecondformatdetail: [],//时分秒

    timesecondformatdetailmill: [],//时分秒
    timesecondformathome: [],//时分
    homebottomtext: [],

    homeTitle: "",

    leftTimePer: 0,
    rightTimePer: 0,
    leftTimeIndex: 0,
    rightTimeIndex: 0,
    t2: 0,
    t3: 0,
    t4: 0,

    detailaver: 0,
    detailaverArr: [0, 0, 0],

    getAverSeries(){
        let y1all = 0
        let y2all = 0
        let y3all = 0

        console.log("length000000000", this.yvalue1.length, this.yvalue2.length, this.yvalue3.length)
        if (this.yvalue1.length > 0) {
            for (let i = 0; i < this.yvalue1.length; i++) {
                y1all += this.yvalue1[i]
                y2all += this.yvalue2[i]

                // if (this.yvalue3[i] < 0) {
                //     console.log("this.yvalue3[i]<0", this.yvalue3[i])
                // } else {
                //     y3all += this.yvalue3[i]
                // }
                //console.log(isNaN(this.yvalue3[i]))
                if (this.yvalue3[i] >= 0) {
                    y3all += this.yvalue3[i]
                } else {

                    console.log("<<<<<<<<<<0", this.yvalue3[i], Math.abs(this.yvalue3[i]))
                    y3all += this.yvalue3[i]
                    console.log("y3all", y3all)

                }
                //  console.log("y3all", y3all)
            }
            this.yvalue1aver = y1all / this.yvalue1.length
            this.yvalue2aver = y2all / this.yvalue2.length
            console.log("lengtdfed", this.yvalue3.length)
            this.yvalue3aver = y3all / (this.yvalue3.length)
        }
        // let m = this.yvalue2.reduce(function (x, y) {
        //     return x + y;
        // });
        console.log(" this.yvalue1aver", this.yvalue1aver)
        console.log(" this.yvalue2aver", this.yvalue2aver)
        console.log(" this.yvalue3aver", this.yvalue3aver)
    },

    getAverByLeftRight(left, right, name){


        console.log(name)
        let list = null;
        let len = 0
        switch (name) {
            case "S1":
                list = eval('(' + JSON.stringify(this.yvalue1) + ')')
                break
            case "S2":
                list = eval('(' + JSON.stringify(this.yvalue2) + ')')
                break
            case "S3":
                list = eval('(' + JSON.stringify(this.yvalue3) + ')')
                break
            case "温湿度":
                list = eval('(' + JSON.stringify(this.air) + ')')
                break
            case "分贝":
                list = eval('(' + JSON.stringify(this.yvalue1) + ')')

                break


        }

        len = list.length

        let leftIndexP = left * 1.0 / len
        let rightIndexP = right * 1.0 / len

        let leftIndex = parseInt((len - 1) * leftIndexP)
        let rightIndex = parseInt((len - 1) * rightIndexP)


        if (name == "温湿度") {

            let airAll = 0;
            let tempAll = 0;
            let wetAll = 0;

            for (let i = leftIndex; i < rightIndex; i++) {
                if (this.air[i] >= 0) {
                    airAll += this.air[i]
                } else {
                    airAll += this.air[i]
                }

                if (this.temp[i] >= 0) {
                    tempAll += this.temp[i]
                } else {
                    tempAll += this.temp[i]
                }

                if (this.wet[i] >= 0) {
                    wetAll += this.wet[i]
                } else {
                    wetAll += this.wet[i]
                }
            }

            this.detailaverArr = [airAll / (rightIndex - leftIndex), tempAll / (rightIndex - leftIndex), wetAll / (rightIndex - leftIndex)]


        } else {
            let all = 0;

            for (let i = leftIndex; i < rightIndex; i++) {
                if (list[i] >= 0) {
                    all += list[i]
                } else {
                    all += 0
                }
            }

            this.detailaver = all / (rightIndex - leftIndex)
        }
        list = null

        //
        // // console.log(this.leftTimePer, this.rightTimePer)

        //
        //

        //
        // console.log(leftIndex, rightIndex, all)
        //
        // this.detailaver = all / (rightIndex - leftIndex)


    },


    getAverEnv(){
        this.envaver = []
        let airall = 0;
        let tempall = 0;
        let wetall = 0;
        if (this.air.length > 0) {
            for (let i = 0; i < this.air.length; i++) {
                airall += this.air[i]
                tempall += this.temp[i]
                wetall += this.wet[i]
            }
            this.envaver.push(airall * 1.0 / this.air.length)
            this.envaver.push(tempall * 1.0 / this.temp.length)
            this.envaver.push(wetall * 1.0 / this.wet.length)
        }
    },


    calcuTimeList(){
        console.log("calcuTimeList", this.datainfo)
        //    'calcuTimeList', { start: 1531411180, timelength: 180, rate: 10, groups: 3 }

        this.homeTitle = this.timeStampToDateTitle(this.datainfo.start * 1000) + "-" + this.timeStampToDateTitle((this.datainfo.start + this.datainfo.timelength) * 1000)

        this.timesecond = []
        this.timesecondformatdetail = []
        this.timesecondformathome = []
        this.homebottomtext = []

        this.timesecond.push(this.datainfo.start)
        for (let secondIndex = 0; secondIndex < this.datainfo.timelength; secondIndex++) {
            this.timesecond.push(this.timesecond[secondIndex] + 1);
        }

        //    console.log("this.timesecond", this.timesecond)


        for (let i = 0; i < this.timesecond.length; i++) {

            this.timesecondformatdetail.push(this.timeStampToDateDetail(this.timesecond[i] * 1000))
            this.timesecondformathome.push(this.timeStampToDateHome(this.timesecond[i] * 1000))
        }
        for (let i = 0; i < this.timesecond.length; i++) {

            this.timesecondformatdetailmill.push(this.timeStampToDateDetail(this.timesecond[i] * 1000))


            for (let i = 0; i < this.datainfo.rate - 1; i++) {
                this.timesecondformatdetailmill.push("")
            }
        }


        //    console.log("this.timesecondformatdetail", this.timesecondformatdetail)


        this.homebottomtext.push(this.timesecondformathome[0])
        //   this.homebottomtext.push(this.timesecondformathome[parseInt(this.timesecondformathome.length * 1.0 / 5)])
        // this.homebottomtext.push(this.timesecondformathome[parseInt(this.timesecondformathome.length * 1.0 / 5 * 2) - 1])
        this.homebottomtext.push(this.timesecondformathome[parseInt(this.timesecondformathome.length * 1.0 / 5 * 3) - 1])
        //this.homebottomtext.push(this.timesecondformathome[parseInt(this.timesecondformathome.length * 1.0 / 5 * 4) - 1])
        this.homebottomtext.push(this.timesecondformathome[this.timesecondformathome.length - 1])
    },

    timeStampToDateDetail(timestamp){

        let date = new Date(timestamp);
        //  let year = date.getFullYear();
        //   let month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        //   let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        let minuts = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        return hours + ":" + minuts + ":" + seconds;
    },
    timeStampToDateHome(timestamp){

        let date = new Date(timestamp);
        let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        let minuts = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        return hours + ":" + minuts
    },

    timeStampToDateTitle(timestamp){

        let date = new Date(timestamp);
        let year = date.getFullYear();
        let month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        let minuts = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
        return month + "/" + day + " " + hours + ":" + minuts;
    },
    getDetailBottom(left, right, length){

        this.leftTimePer = left * 1.0 / length
        this.rightTimePer = right * 1.0 / length

        // console.log(this.leftTimePer, this.rightTimePer)


        this.leftTimeIndex = parseInt((this.timesecondformatdetail.length - 1) * this.leftTimePer)
        this.rightTimeIndex = parseInt((this.timesecondformatdetail.length - 1) * this.rightTimePer)


        // console.log("timesecondformatdetail timesize", this.timesecondformatdetail.length)
        // console.log("timesecond timesize", this.timesecond.length)
        //
        // console.log(this.leftTimeIndex, this.rightTimeIndex)

        this.t2 = this.leftTimeIndex + parseInt((this.rightTimeIndex - this.leftTimeIndex) * 1.0 / 4)
        this.t3 = this.leftTimeIndex + parseInt((this.rightTimeIndex - this.leftTimeIndex) * 1.0 / 4 * 2)
        this.t4 = this.leftTimeIndex + parseInt((this.rightTimeIndex - this.leftTimeIndex) * 1.0 / 4 * 3)

        return [this.timesecondformatdetail[this.leftTimeIndex], this.timesecondformatdetail[this.t2], this.timesecondformatdetail[this.t3], this.timesecondformatdetail[this.t4], this.timesecondformatdetail[this.rightTimeIndex]]
    },
    splitByConLength(str, n){
        let ret = []

        for (let i = 0; i < parseInt(str.length * 1.0 / n) - 1; i++) {
            ret.push(str.substring(i * n, i * n + n))
        }

        return ret
    },
    getPath(name){
        return (Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : RNFS.ExternalStorageDirectoryPath ) + '/' + name;
    }


}
